<?php

namespace app\common\library\job;

use think\Log;
use think\queue\Job;
use think\Db;


class OvertimeJob extends BaseJob

{

     /**
      * 完成任务后检测超时审核
      * 
      * $data['orderId']  任务订单id
      *  \think\Queue::later(($confirm_days * 86400), '\app\common\library\job\OvertimeJob@orderTimeout', $data, 'ordertimeout');
      * 
     */
    
    
    
    public function orderTimeout(Job $job, $data){
        try {
            $orderId = $data['orderId'];

            // 重新查询订单
            $order = Db::name("expert_upgrade_order")
                    ->field("o.id,u.nickname,u.avatar,u.mobile,u.unique_id,o.status,o.up_user_id,o.order_type,o.upgrade_type,o.expiretime,o.order_sn,o.order_amount,o.user_id")
                    ->alias('o')
                    ->join('user u', 'u.id=o.up_user_id')
                    ->where(['o.id'=>$orderId])
                    ->find();

            if (empty($order)) {
                Log::write("不存在的订单 id=".$orderId, "orderTimeout");
                $job->delete();
            }
            
            if($order['expiretime']> time()){
                $laterTime =$order['expiretime']- time();
                //延迟2秒后再执行
                $job->release($laterTime);
            }
            
            if($order['status']==1){ //待审核的订单
                    Db::transaction(function () use ($order) {
                        Db::name('user')->where(['id'=>$order['up_user_id']])->setInc('expire_times');
                        Db::name("expert_upgrade_order")->data(['status'=>2,'is_expire_time'=>1])->where(['id'=>$order['id']])->update();
                        \app\admin\model\ExpertUpgradeOrder::hideUser($order['up_user_id']);
                    });
                        //查询是否完成全部任务,完成给他升级
                        $ExpertUpgradeOrder = new \app\admin\model\ExpertUpgradeOrder();
                        $ret = $ExpertUpgradeOrder->upLevel($order['user_id']);
                        if(!$ret){
                            \think\Log::write("审核升级失败！".__FILE__.'行：'.__LINE__);
                            //如果失败再丢给队里执行一边
                            \think\Queue::push("\app\common\library\job\OvertimeJob@upUserLevel", ['up_user_id'=>$order['user_id']], 'ordertimeout');
                        }
                     $job->delete();
            } //其他的删除队列
            // 删除 job
            $job->delete();
        } catch (\Exception $e) {
//            var_dump($e);
            \think\Log::write("审核升级失败！".$e->getMessage().'行：'.$e->getFile().':'.$e->getLine());
            // 队列执行失败
            if ($job->attempts() > 3) {
                 $job->failed($data);
            }
                //延迟2秒后再执行
                $job->release(2);
        }
    }
    
     /**
      * 完成任务后检测超时审核
      * 
      * $data['up_user_id']  需要升级的用户user_id
      *  \think\Queue::later(($confirm_days * 86400), '\app\common\library\job\OvertimeJob@upUserLevel', $data, 'ordertimeout');
      * 
     */
    
    public function upUserLevel(Job $job, $data) {

        //查询是否完成全部任务,完成给他升级
        $ExpertUpgradeOrder = new \app\admin\model\ExpertUpgradeOrder();
        $ret = $ExpertUpgradeOrder->upLevel($data['up_user_id']);
        if (!$ret) {
             \think\Log::write("审核升级失败！".__FILE__.'行：'.__LINE__);
            //如果失败再丢到失败方法记录；后面排除问题
            $job->failed($data);
        }
        $job->delete();
    }

}
